package org.example.service.impl;

import cn.hutool.core.date.DateUtil;
import org.example.dao.StudentDao;
import org.example.dto.StudentDto;
import org.example.pojo.Classes;
import org.example.pojo.Student;
import org.example.service.StudentService;
import org.example.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class StudentDaoImpl implements StudentService {

    @Autowired
    private StudentDao dao;


    private StudentDto PoTransToDto(Student student){
        StudentDto studentDto=new StudentDto();
        studentDto.setStudentId(student.getAccount());
        studentDto.setStudentName(student.getName());
        studentDto.setDepartment(student.getClasses().getDepartname());
        studentDto.setClassname(student.getClasses().getClassname());
        studentDto.setBirthday(student.getDate());
        studentDto.setEmail(student.getEmail());
//        studentDto.setGrade(String.valueOf(student.getAccount()).substring(0,2)+student.getClasses().getMajorname());
        studentDto.setGrade("19软件");
        studentDto.setAddress("0");
        studentDto.setUnionAddress("0");
        studentDto.setCity("厦门");
        studentDto.setMajor(student.getClasses().getMajorname());
        studentDto.setDistrict("集美");
        studentDto.setProvince("福建");
        studentDto.setPhone(student.getTelenumber());
        return studentDto;
    }

    private Student VoTransToPo(StudentVo studentVo){
        Student student=new Student();
        student.setDate(studentVo.getBirthday());
        Classes classes=new Classes();
        classes.setClassname(studentVo.getClassname());
        student.setClasses(classes);
        student.setAccount(studentVo.getStudentId());
        student.setEmail(studentVo.getEmail());
        student.setJoindate(String.valueOf(DateUtil.date()));
        student.setName(studentVo.getStudentName());
        student.setPassword(String.valueOf(studentVo.getStudentId()).substring(0,10));
        student.setTelenumber(studentVo.getPhone());

        return student;
    }

    @Override
    public Integer addStudent(StudentVo studentVo) {
        if(dao.insert(VoTransToPo(studentVo)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateStudent(StudentVo studentVo) {
        if(dao.update(VoTransToPo(studentVo)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteStudent(Integer studentId) {
        if(dao.delete(studentId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<StudentDto> queryAllStudent() {
        List<Student> studentList=dao.queryAll();
        List<StudentDto> studentDtoList=new ArrayList<>();
        for(int i=0;i<studentList.size();i++)
            studentDtoList.add(i,PoTransToDto(studentList.get(i)));
        return studentDtoList;
    }

    @Override
    public Integer countStudent() {
        return dao.queryAll().size();
    }

    @Override
    public StudentDto queryStudentById(Integer studentId) {
        Student student=new Student();
        student.setAccount(studentId);
        return PoTransToDto(dao.queryByCondition(student).get(0));
    }

    @Override
    public StudentDto queryStudentByName(String studentName) {
        Student student=new Student();
        student.setName(studentName);
        return PoTransToDto(dao.queryByCondition(student).get(0));
    }

    @Override
    public List<StudentDto> queryStudentByDepartmentAndMajorAndGradeAndClass(String department, String major, String grade, String classes) {
        Student student=new Student();
        Classes classes1=student.getClasses();
        classes1.setDepartname(department);
        classes1.setMajorname(major);
        //这里有错
        classes1.setClassname(classes);
        student.setClasses(classes1);

        List<Student> studentList=dao.queryByCondition(student);
        List<StudentDto> studentDtoList=new ArrayList<>();
        for(int i=0;i<studentList.size();i++)
            studentDtoList.add(i,PoTransToDto(studentList.get(i)));
        return studentDtoList;
    }

    @Override
    public Integer countStudentByDepartmentAndMajorAndGradeAndClass(String department, String major, String grade, String classes) {
        Student student=new Student();
        Classes classes1=student.getClasses();
        classes1.setDepartname(department);
        classes1.setMajorname(major);
        //这里有错
        classes1.setClassname(classes);
        student.setClasses(classes1);
        return dao.queryByCondition(student).size();
    }

}
